package shuashuami.hb.com.avtivity.agent;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.avtivity.AbActivity;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.manager.agent.UserManager;

public class WodeyuEActivity extends AbActivity {

    private TextView tvYuE;

    private Button btnTixian;
    private LinearLayout llMingxi, llJilu;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_cwodeyu_e);
        tvYuE = (TextView) findViewById(R.id.tv_cuser_my_yue_money);
        btnTixian = (Button) findViewById(R.id.btn_cuser_yue_tixian);
        llMingxi = (LinearLayout) findViewById(R.id.ll_wodeyue_yongjinmingxi);
        llJilu = (LinearLayout) findViewById(R.id.ll_wodeyue_tixianjilu);
    }

    @Override
    protected void initData() {
        setTitleBar("我的余额");
        setLeftView();
        getMessage();
        tvYuE.setText(UserManager.getInstance().getUser().getMoney() + "");
    }

    @Override
    protected void initListener() {
        btnTixian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WodeyuEActivity.this, ShenqingTixianActivity.class);
                startActivity(intent);
            }
        });
        llMingxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WodeyuEActivity.this, YongJinMingxiActivity.class);
                startActivity(intent);
            }
        });
        llJilu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WodeyuEActivity.this, TixianjiluActivity.class);
                startActivity(intent);
            }
        });
    }

    public void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        HttpAgentMethods.getYuE(listener);
//        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(WodeyuEActivity.this);
//        HttpCMethods.getYuE(listener,
//                shareCUserInfo.getId()
//        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(WodeyuEActivity.this);
                            loginHttp.login();
                            return;
                        }
                        JSONObject temp = jsonObject.getJSONObject("content");
                        tvYuE.setText(UserManager.getInstance().getUser().getMoney() + "");

                    } catch (JSONException e) {
                        ToastUtil.showShort(WodeyuEActivity.this, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };
}
